package com.ios.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.ios.dto.DisablerExecution;
import com.ios.dto.RelativeExecution;
import com.ios.entity.DisablerUser;
import com.ios.entity.RelativeUser;
import com.ios.enums.DisablerStateEnum;
import com.ios.enums.RelativeStateEnum;
import com.ios.service.IDisablerService;
import com.ios.service.IRelativeService;
import com.ios.util.HttpServletRequestUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
/*POST:一般是用于提交数据的

        GET:一般是用于获取数据的

        PUT:一般是用于更新一条数据的

        DELETE:一般是用于删除数据的

        PATCH:一般是用于更新一批数据的*/
@Controller
@RequestMapping(value = "/select",method = {RequestMethod.GET})
public class SelectController {

    @Autowired
    private IDisablerService disablerService;
    @Autowired
    private IRelativeService relativeService;

    @RequestMapping(value = "/disabler")
    @ResponseBody
    public Map<String,Object> QueryDisabler(HttpServletRequest request){
        Map<String,Object> modelMap = new HashMap<String,Object>();
        /*接收并转化相应的参数*/
        String username = HttpServletRequestUtil.getString(request, "username");
        DisablerUser disablerUser = new DisablerUser();
        try{
            disablerUser.setUsername(username);
        }catch (Exception e){
            modelMap.put("success",false);
            modelMap.put("errMsg1",e.getMessage());
            return modelMap;
        }
        /*查询并返回*/
        if (disablerUser != null){
            DisablerExecution de;
            try{
                de = disablerService.QueryDisablerUser(username);
                if(de.getState() == DisablerStateEnum.SUCCESS.getState()){
                    modelMap.put("success",true);
                    modelMap.put("disablerInfo",de.getDisablerUser());
                }else {
                    modelMap.put("success",false);
                    modelMap.put("errMsg2",de.getStateInfo());
                }
            }catch (Exception e){
                modelMap.put("success",false);
                modelMap.put("errMsg3",e.getMessage());
            }
            return modelMap;
        }else {
            modelMap.put("success",false);
            modelMap.put("errMsg4","请输入要查询的用户名");
            return modelMap;
        }
    }

    @RequestMapping(value = "/relative")
    @ResponseBody
    public Map<String,Object> QueryRelative(HttpServletRequest request){
        Map<String,Object> modelMap = new HashMap<String,Object>();
        /*接收并转化相应的参数*/
        String username = HttpServletRequestUtil.getString(request, "username");
        RelativeUser relativeUser = new RelativeUser();
        try{
            relativeUser.setUsername(username);
        }catch (Exception e){
            modelMap.put("success",false);
            modelMap.put("errMsg",e.getMessage());
            return modelMap;
        }
        /*查询并返回*/
        if (relativeUser != null){
            try{
                RelativeExecution re = relativeService.QueryRelativeUser(username);
                if(re.getState() == RelativeStateEnum.SUCCESS.getState()){
                    modelMap.put("success",true);
                    modelMap.put("relativeInfo",re.getRelativeUser());
                    System.out.println(re.getRelativeUser());
                }else {
                    modelMap.put("success",false);
                    modelMap.put("errMsg",re.getStateInfo());
                }
            }catch (Exception e){
                modelMap.put("success",false);
                modelMap.put("errMsg",e.getMessage());
            }
            return modelMap;
        }else {
            modelMap.put("success",false);
            modelMap.put("errMsg","请输入要查询的用户名");
            return modelMap;
        }
    }

}
